
#include "network/mqtt/studio_mqtt_client.h"
#include "network/mqtt/studio_mqtt_server.h"
#include <chrono> // 操作时间
#include <thread>
#include "json/studio_json.h"

#include <iostream>
#include <cstdlib>
#include <string>
#include <thread>
#include <chrono>
#include <mqtt/client.h>



int main()
{
    studio_mqtt_client client;
    client.client_id("mqttx_cb2be766bbbb");
    client.protocol("mqtt");
    client.host("81.70.146.224");
    client.port(1883);
    client.user("/car:car");
    client.password("NZ852ijd169@");
    client.topic_name("test/car");


    std::string message = "Hello, MQTT!";
    //client.publish(client.m_topic_name, message);

    client.subscribe();




    // 发布的数据
    Json::Value root;  // JSON 根节点
    root["id"] = 12345;
    root["name"] = "Test Device";
    root["status"] = "active";
    root["timestamp"] = "2025-04-22T10:22:00Z";
    Json::StreamWriterBuilder writer;
    std::string json_payload = Json::writeString(writer, root);



    // 发布消息
    int index = 1;
    while (true)
    {
        std::string message;
        message = "Hello, MQTT! " + std::to_string(index);
        index ++ ;

        bool success = client.publish("/test/json_topic_wyj", message);
        if (success)
        {
            std::cout << "JSON 数据发布成功: " << message << std::endl;
        }
        else
        {
            std::cerr << "JSON 数据发布失败!" << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::seconds(2));  // 秒
    }

	return 0;
}


